const express = require('express');
const asyncHandler = require('express-async-handler');
const basicControl = require('../controllers/basic.controller');
const Busboy = require('connect-busboy')
const FormData = require('form-data')
const router = express.Router();
module.exports = router;

router.post('/getLiveStreamUrl', asyncHandler(getLiveStreamUrl))
router.post('/getRobotInfos', asyncHandler(getRobotInfos))
router.post('/uploadFile', Busboy({ defParamCharset: 'utf8' }), asyncHandler(uploadFile))
router.post('/downloadFile', asyncHandler(downloadFile))
router.post('/getAllFileList', asyncHandler(getAllFileList))
router.post('/addNewDirPath',asyncHandler(addNewDirPath))
router.post('/getWorkingAreaPersonNumData',asyncHandler(getWorkingAreaPersonNumData))

async function getLiveStreamUrl(req, res) {
    let result = await basicControl.getLiveStreamUrl(req.body)
    res.json(result)
}

async function getRobotInfos(req, res) {
    let result = await basicControl.getRobotInfos(req.body)
    res.json(result)
}

async function uploadFile(req, res) {
    let body = {}
    req.pipe(req.busboy)
    req.busboy.on("field", (key, value) => {
        console.log(`key:${key}, value:${value}`)
        body[key] = value
    })
    req.busboy.on("file", (name, file, info, encoding, mimetype) => {
        let form = new FormData()
        form.append('tag', body.tag)
        form.append('unionId', body.unionId)
        form.append('file', file, { filename: info.filename, encoding: info.encoding, contentType: info.mimetype })
        basicControl.uploadFileToOSS(req, res, form)
    })
}

async function downloadFile(req, res) {
    let result = await basicControl.downloadFile(req.body)
    // console.log(result)
    result.pipe(res)
}

async function getAllFileList(req, res) {
    let result = await basicControl.getAllFileList(req.body)
    res.json(result)
}

async function getWorkingAreaPersonNumData(req,res){
    let result = await basicControl.getWorkingAreaPersonNumData(req.body)
    res.json(result)
}

async function addNewDirPath(req,res){
    let result = await basicControl.addNewDirPath(req.body)
    res.json(result)
}